package com.ruoyi.common.utils.translate;

import java.util.HashMap;
import java.util.Map;

public class TransApi {
    private static final String TRANS_API_HOST = "http://api.fanyi.baidu.com/api/trans/vip/translate";

    private String appid;
    private String securityKey;

    public TransApi(String appid, String securityKey) {
        this.appid = appid;
        this.securityKey = securityKey;
    }

    private static final String APP_ID = "20231204001900524";
    private static final String SECURITY_KEY = "OnaTbvenGtOK0NawlfG2";

    public static final String KOR_LANG = "kor"; // 韩语
    public static final String TH_LANG = "th";// 泰语
    public static final String PT_LANG = "pt";  // 葡萄牙语
    public static final String EL_LANG = "el";// 希腊语
    public static final String BUL_LANG = "bul";// 保加利亚语
    public static final String FIN_LANG = "fin";// 芬兰语
    public static final String SLO_LANG = "slo";// 斯洛文尼亚语
    public static final String CHT_LANG = "cht";// 繁体
    public static final String ZH_LANG = "zh";// 中文
    public static final String FRA_LANG = "fra";// 法语
    public static final String ARA_LANG = "ara";// 阿拉伯语
    public static final String DE_LANG = "de";// 德语
    public static final String NL_LANG = "nl"; // 荷兰语
    public static final String EST_LANG = "est";// 爱沙尼亚语
    public static final String CS_LANG = "cs";// 捷克语
    public static final String SWE_LANG = "swe";// 瑞典语
    public static final String VIE_LANG = "vie";// 越南语
    public static final String EN_LANG = "en";// 英文
    public static final String JA_LANG = "ja";// 日语
    public static final String SPA_LANG = "spa";// 西班牙语
    public static final String RU_LANG = "ru";// 俄语
    public static final String IT_LANG = "it";// 意大利语
    public static final String PL_LANG = "pl";// 波兰语
    public static final String DAN_LANG = "dan";// 丹麦语
    public static final String ROM_LANG = "rom";// 罗马尼亚语
    public static final String HU_LANG = "hu";// 匈牙利语

    public String getTransResult(String query, String from, String to) {
        Map<String, String> params = buildParams(query, from, to);
        return HttpGet.get(TRANS_API_HOST, params);
    }

    private Map<String, String> buildParams(String query, String from, String to) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);

        params.put("appid", appid);

        // 随机数
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);

        // 签名
        String src = appid + query + salt + securityKey; // 加密前的原文
        params.put("sign", MD5.md5(src));

        return params;
    }

}
